<?php

class content extends base {

    private $content;
    private $files = NULL;

    public function __construct() {
        global $config;
        if (!class_exists("files"))
            require_once "../functions/files.php";
        $this->files = new files($config);
    }

    public function submenu() {
        $this->submenu = array();
        $this->submenu[0]["name"] = "Show";
        $this->submenu[0]["callback"] = "show";
        $this->submenu[0]["icon"] = "icons/cross.png";
        $this->submenu[0]["rights"] = 0;
        $this->submenu[0]["enabled"] = true;
        $this->_submenu($this->submenu);
    }

    public function delete_all() {
        $action = $_REQUEST["action"];
        if ($action == "form_downloaded")
            $r = $this->db->delete_rows("downloaded"); else
        if ($action == "form_torrent")
            $r = $this->db->delete_rows("torrent");
        return $r;
    }

    public function delete() {
        foreach ($_REQUEST as $req => $val) {
            if (substr($req, 0, 3) == "id_")
                $ids[] = substr($req, 3);
        }
        if (empty($ids))
            return false;
        foreach ($ids as $id) {
            $action = $_REQUEST["action"];
            if ($action == "form_downloaded")
                $r = $this->db->delete_rows("downloaded", "id = {$id}"); else
            if ($action == "form_torrent")
                $r = $this->db->delete_rows("torrent", "id = {$id}");
        }
        return true;
    }

    private function import() {
        global $config;
        $dest_movies = $config["Directories"]["destination"];
        $dest_tv = $config["Directories"]["destination_tv"];
        $content = $this->files->get_folder($dest_movies);
        if (!$content)
            return false;
        foreach ($content as $item) {
            $name = strtolower($item["name"]);
            $matched = preg_match('/(19|20)\d{2}\b/i', $name, $match);
            if (!$matched)
                continue;
            $year = $match[0];
            $ys = strpos($name, "({$year})");
            if (!$ys)
                continue;
            if (strpos($name, ",the"))
                $name = "the " . str_replace(",the", "", $name);
            $name_clean = substr($name, 0, $ys - 1);
            $rows = array(array("name" => $name, "imdb_year" => $year, "download_date" => microtime(true)));
            $r = $this->db->insert_rows("downloaded", $rows);
        }
    }

    public function show() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Delete_All"]))
            $this->delete_all(); else
        if (isset($this->requests["Delete"]))
            $this->delete(); else
        if (isset($this->requests["Import_from_source"]))
            $this->import();

        $this->page_manager->text_title("Downloaded");
        $header = array(
            array("title" => "ID", "color" => "green"),
            array("title" => "Date", "color" => "green"),
            array("title" => "Name", "color" => "green"),
            array("title" => "TV Season", "color" => "green"),
            array("title" => "TV Episode", "color" => "green"),
            array("title" => "Size", "color" => "green"),
            array("title" => "Quality", "color" => "green"),
            array("title" => "Year", "color" => "green"),
            array("title" => "Country", "color" => "green"),
            array("title" => "Actors", "color" => "green"),
            array("title" => "Synopsis", "color" => "green"),
            array("title" => "Release Date", "color" => "green"),
//            array("title" => "Link", "color" => "green"),
            array("title" => "Rating", "color" => "green"),
//            array("title" => "Picture", "color" => "green"),
            array("title" => "Genres", "color" => "green"),
        );
        $mrows = array();
        $rows = $this->db->get_rows("downloaded");
        foreach ($rows as $row) {
            $row["size"] = $this->bytes_human_readable($row["size"]);
            if (!empty($row["imdb_synopsis"]))
                $row["imdb_synopsis"] = "<a href='" . $row["imdb_link"] . "' title='{$row["imdb_synopsis"]}'>" . substr($row["imdb_synopsis"], 0, 50) . "...</a>";
            else
                $row["imdb_synopsis"] = "N/A";
            if (!empty($row["imdb_actors"]))
                $row["imdb_actors"] = "<a href='" . $row["imdb_link"] . "' title='{$row["imdb_actors"]}'>" . substr($row["imdb_actors"], 0, 50) . "...</a>";
            else
                $row["imdb_actors"] = "N/A";
            if (!empty($row["name"]))
                $row["name"] = "<a href='" . $row["imdb_link"] . "'>{$row["name"]}</a>";
            else
                $row["name"] = "N/A";
            if (!empty($row["imdb_picture"]))
                $row["imdb_picture"] = "<img src='" . $row["imdb_picture"] . "'>";
            else
                $row["imdb_picture"] = "N/A";
            unset($row["imdb_picture"]);
            unset($row["imdb_link"]);
            $row["download_date"] = date("Y-m-d H:i:s", $row["download_date"]);
            $mrows[] = $row;
        }
        $button = array("Delete", "Delete All", "Import from source");

        $this->page_manager->start_form("form_downloaded");
        $this->page_manager->form_add_field("action", "action", "action", "form_downloaded", "hidden");
        $this->page_manager->style_table($header, $mrows, true, $button, 1000, "show");
        $this->page_manager->end_form();

        $this->page_manager->text_title("Torrent");
        $header = array(
            array("title" => "ID", "color" => "green"),
            array("title" => "Date", "color" => "green"),
            array("title" => "Link", "color" => "green"),
            array("title" => "Size", "color" => "green"),
            array("title" => "Seeds", "color" => "green"),
            array("title" => "Peers", "color" => "green"),
            array("title" => "Name", "color" => "green"),
            array("title" => "TV Season", "color" => "green"),
            array("title" => "TV Episode", "color" => "green"),
            array("title" => "Quality", "color" => "green"),
            array("title" => "Year", "color" => "green"),
            array("title" => "Country", "color" => "green"),
            array("title" => "Actors", "color" => "green"),
            array("title" => "Synopsis", "color" => "green"),
            array("title" => "Release Date", "color" => "green"),
//            array("title" => "Link", "color" => "green"),
            array("title" => "Rating", "color" => "green"),
//            array("title" => "Picture", "color" => "green"),
            array("title" => "Genres", "color" => "green"),
        );
        $mrows = array();
        $rows = $this->db->get_rows("torrent");
        foreach ($rows as $row) {
            $row["size"] = $this->bytes_human_readable($row["size"]);
            if (!empty($row["imdb_synopsis"]))
                $row["imdb_synopsis"] = "<a href='" . $row["imdb_link"] . "' title='{$row["imdb_synopsis"]}'>" . substr($row["imdb_synopsis"], 0, 35) . "...</a>";
            else
                $row["imdb_synopsis"] = "N/A";
            if (!empty($row["imdb_actors"]))
                $row["imdb_actors"] = "<a href='" . $row["imdb_link"] . "' title='{$row["imdb_actors"]}'>" . substr($row["imdb_actors"], 0, 35) . "...</a>";
            else
                $row["imdb_actors"] = "N/A";
            if (!empty($row["name"]))
                $row["name"] = "<a href='" . $row["imdb_link"] . "'>{$row["name"]}</a>";
            else
                $row["name"] = "N/A";
            if (!empty($row["imdb_picture"]))
                $row["imdb_picture"] = "<img src='" . $row["imdb_picture"] . "'>";
            else
                $row["imdb_picture"] = "N/A";
            unset($row["imdb_picture"]);
            unset($row["imdb_link"]);
            $row["torrent_date"] = date("Y-m-d H:i:s", $row["torrent_date"]);
            $row["link"] = "<a href='" . $row["link"] . "'>LINK</a>";
            $mrows[] = $row;
        }
        $button = array("Delete", "Delete All");

        $this->page_manager->start_form("form_torrent");
        $this->page_manager->form_add_field("action", "action", "action", "form_torrent", "hidden");
        $this->page_manager->style_table($header, $mrows, true, $button, 1000, "show");
        $this->page_manager->end_form();

        return $this->page_manager->web_content;
    }

    private function bytes_human_readable($bytes) {
        if ($bytes < 1000)
            $bytes = $bytes . "B"; else
        if ($bytes < 1000000)
            $bytes = ROUND($bytes / 1000) . "KB"; else
        if ($bytes < 1000000000)
            $bytes = ROUND($bytes / 1000000) . "MB"; else
        if ($bytes < 1000000000000)
            $bytes = ROUND($bytes / 1000000000) . "GB"; else
        if ($bytes < 1000000000000000)
            $bytes = ROUND($bytes / 1000000000000) . "TB";
        return $bytes;
    }

    private function update_config($config) {
        $requests = $this->requests;
        $page = $requests["page"];
        $submenu = $requests["submenu"];
        unset($requests["page"]);
        unset($requests["submenu"]);
        unset($requests["Change"]);
        foreach ($requests as $request => $value) {
            $config[$submenu][$request] = $value;
            $this->db->delete_rows("settings", "section='{$submenu}' AND name='{$request}'");
            $rows[0] = array("section" => $submenu, "name" => $request, "value" => $value);
            $r = $this->db->insert_rows("settings", $rows);
        }
        return $config;
    }

    public function run() {
        $this->submenu();
        $callback = $this->_run();
        if ($callback)
            $this->content = $this->$callback();
        $this->finish($this->content);
    }

}

?>